package common.fengkai.filepost.test;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.codegen.mybatis3.model.ExampleGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Fengkai
 * @creat 2023/6/22
 */
public class MyGenerator extends MyBatisGenerator {
    /**
     * 自动生成代码
     */
    public MyGenerator (Configuration configuration, ShellCallback shellCallback, List<String> warnings) throws InvalidConfigurationException {
        super(configuration, shellCallback, warnings);
    }

    public void generate() throws SQLException, IOException, InterruptedException {
        super.generate(null);

    }

    public static void generatorForTableName (String tablename, String domainObjectName) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src\\main\\resources\\mybatis-generator-config.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);


        List<TableConfiguration> tableConfigurations = config.getContexts().get(0).getTableConfigurations();
        tableConfigurations.get(0).setTableName(tablename);
        tableConfigurations.get(0).setDomainObjectName(domainObjectName);

        MyGenerator myBatisGenerator = new MyGenerator(config, callback, warnings);
        myBatisGenerator.generate();
    }
}
